from django.http.request import QueryDict
from django.core.exceptions import ObjectDoesNotExist
from cmdb.models import Host
from accounts.models import Profile
from cmdb.backends.crypto import RsaCrypto


def args(pk, user, query):
    _t = Host.objects.get(id=pk)

    if _t.password:
        state, data = RsaCrypto().decrypt(_t.password)
        if state:
            password = data
        else:
            return False, data
    else:
        password = None

    try:
        pkey = Profile.objects.get(user=user).pkey
    except ObjectDoesNotExist:
        pkey = None

    _q = QueryDict(query_string=query, encoding='utf-8')

    ssh_connect_args = {
        'host': _t.host,
        'port': _t.port,
        'username': _t.username,
        'authtype': _t.authtype,
        'password': password,
        'pkey': pkey,
        'cols': int(_q.get('cols')),
        'rows': int(_q.get('rows'))
    }

    return ssh_connect_args


if __name__ == '__main__':
    print('ops-coffee.cn')
